package com.jarich.morewore.model;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.webkit.WebView;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

public class MyOverlayItem extends ItemizedOverlay {

	public int id;
	public String plaatsNaam;
	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	Context mContext; 
	
	public MyOverlayItem(Drawable defaultMarker) {
		super(boundCenterBottom(defaultMarker));
	}
	
	public MyOverlayItem(Drawable defaultMarker, Context context, int id) {
		super(boundCenterBottom(defaultMarker));
		mContext = context;
		this.id = id;
	}
	
	public void setDrawable(Drawable d)
	{
		mOverlays.get(0).setMarker(d);
	}
	
	@Override
	protected boolean onTap(int index)
	{
		OverlayItem item = mOverlays.get(index);
		System.out.println(index + "  " + item.getTitle());
		if(item.getTitle().equals("Dit is uw huidige locatie"))
		{
			return false;
		}
		/*AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
		dialog.setTitle(item.getTitle());
		dialog.setMessage(item.getSnippet());
		dialog.show();*/
		new GetDataTask().execute(item.getTitle());
		return true;
	}
	
	public void addOverlay(OverlayItem overlay) {
	    mOverlays.add(overlay);
	    populate();
	}

	@Override
	protected OverlayItem createItem(int i) {
		return mOverlays.get(i);
	}

	@Override
	public int size() {
		return mOverlays.size();
	}
	
	private class GetDataTask extends AsyncTask<String, Void, String> {
       	@Override
        protected void onPostExecute(String param) {
       		AlertDialog.Builder alert = new AlertDialog.Builder(mContext);
       		WebView wv = new WebView(mContext);
       		wv.loadData(param, "text/html", "UTF-8");
       		alert.setView(wv);
       		alert.show();
       	}

    		@Override
    		protected String doInBackground(String... params) {
    			Weerbericht w = new Weerbericht();
                String s = w.getWeerbericht(w.getWOEID(params[0]));
                return "<h>"+params[0]+"</h><br/>"+s;
    		}
       }
}
